package com.lumlord.convert;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

@Component
public class StringToDateConverter implements Converter<String, Date> {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private final String[] FORMATES = { "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy年MM月dd日  HH时mm分ss秒",
			"yyyy-MM-dd", "yyyy/MM/dd", "yyyy年MM月dd日" };

	@Override
	public Date convert(String source) {
		for (String format : FORMATES) {
			DateFormat df = new SimpleDateFormat(format);
			df.setTimeZone(TimeZone.getDefault());
			try {
				return df.parse(source);
			} catch (ParseException e) {
				logger.debug(e.getMessage(), e);
			}
		}
		return null;
	}

}
